<?php

namespace App\Http\Controllers\Admin;

use App\Models\MemberOther;
use App\Models\NewAdmin;
use App\Models\Order;
use App\Models\OrderCredit;
use App\Models\OrderLease;
use App\Models\OrderLeaseAudit;
use App\Models\OtherOrder;
use App\Models\OtherOrderAllocate;
use App\Models\OtherOrderSet;
use App\Models\RiskCredit;
use App\Services\AlipayRiskService;
use App\Services\AntchainAtoService;
use App\Services\AntchainService;
use App\Services\AuthService;
use App\Services\CommonService;
use App\Services\CreditCallService;
use App\Services\OrderService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Response;
use Illuminate\Http\Response as SymfonyResponse;

class PushOrderController
{
    private $oneITime = 1; // 单位时间 一分钟
    private $oneIMax = 1; // 一个用户Ip一分钟最多仅允许访问访问10次

    private $platITime = 1; // 针对所有用户，单位时间内允许访问的最大次数
    private $platIMax = 1; // 针对所有的用户,该接口最多仅允许访问N次


    //外部订单推送
    public function handle(Request  $request)
    {
            $redis = new \redis();
        $userid = $_SERVER['HTTP_USER_AGENT'];
        $redis->connect("127.0.0.1");
        $redis->select(0);
        //.... 针对平台全局锁，用于限制单位时间内所有用户访问的次数
        $platKey = md5($request->path());
        $platFlowCount = $redis->get($platKey);// 平台访问次数
        if($platFlowCount){
            if($platFlowCount >= $this->platIMax){
                return [
                    'status'=>100,
                    'msg' =>"平台超过限制"
                ];
            }
        }
        $redis->incr($platKey);// ++1 次访问次数
        !$platFlowCount && $redis->expire($platKey,$this->platITime); // 设置锁的有效时间

        $ip=$request->getClientIp();
        // ... 针对单个用户实现的访问锁
        $key = md5($ip.":".$userid);
        // 实现一个用户一分钟最多允许访问10次
        $userFlowCount = $redis->get($key); // 单个用户访问次数
        if($userFlowCount){
            if($userFlowCount >= $this->oneIMax){
                return [
                    'status'=>100,
                    'msg' =>"用户访问超过限制"
                ];
            }
        }
        $redis->incr($key);// ++1 次访问次数
        !$userFlowCount && $redis->expire($key,$this->oneITime);//设置锁的有效时间

    }





}
